// pages/invoice/invoice.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentIndex: 0, //默认是活动项
    list: [],
    receivedList:[],//待开具
    dealedList:[],//待处理
    patintIdS: '',
    stateDataId:'',
     
  },
  //开票
  invoiceOn:function(id){
    wx.showLoading({
      title: '开票中',
      mask: true
    });
    //订单编号
    const orderId = id.currentTarget.dataset.id;
    const typeId = id.currentTarget.dataset.typeid;
    const totalPrice = id.currentTarget.dataset.totalprice;
    const status = id.currentTarget.dataset.status;
    const isInvoice = id.currentTarget.dataset.isinvoice;
    wx.request({
      url: 'http://192.168.2.18:9999/staffOrder/generationBill',
      method:"POST",
      data:{
        "orderId":orderId,
        "userEmail":wx.getStorageSync('email'),
        "serverName":"话费充值",
        "type":"话费",
        "typeId":typeId,
        "totalPrice":totalPrice,
        "status":status,
        "isInvoice":isInvoice,
        "name":wx.getStorageSync('name')
        // "userEmail":
      },
      header:{
        "token":wx.getStorageSync('token')
      },
      success:(res)=>{
        console.log(res);
        wx.showToast({
          title: '开票成功',
          icon: 'success',
          duration: 1000
        })
        wx.hideLoading();
      },
      fail:(res)=>{
        wx.request({
          url: 'http://192.168.2.18:9999/productOrder/updateIsInvoiceStatus?orderNum='+orderId,
          "method":"GET",
          header:{  "token":wx.getStorageSync('token')},
          success:(res)=>{
            wx.hideLoading();
            wx.showToast({
              title: '开票成功',
              icon: 'success',
              duration: 1000
            })
            console.log("状态更新");
            wx.reLaunch({
              url: 'pages/invoice/invoice', // 替换为你要跳转的页面路径
            });
          }
        })
      }
    })

  },
  //查看发票
  viewPDF:function(data){
    //获取id
    const orderId = data.currentTarget.dataset.id;
      // 显示加载提示
      wx.showLoading({
        title: '加载中',
        mask: true
      });
  
      // 下载 PDF 文件
      wx.downloadFile({
        url: 'http://192.168.2.18:9000/tickets/'+orderId+'.pdf',
        success: function(res) {
          const filePath = res.tempFilePath;
          // 打开 PDF 文件
          wx.openDocument({
            filePath: filePath,
            fileType: 'pdf',
            success: function() {
              console.log('打开 PDF 成功');
            },
            fail: function(error) {
              console.error('打开 PDF 失败', error);
            },
            complete: function() {
              // 隐藏加载提示
              wx.hideLoading();
            }
          });
        },
        fail: function(error) {
          console.error('下载 PDF 失败', error);
          // 隐藏加载提示
          wx.hideLoading();
        }
      });
  },
  titleClick: function (e) {
    var that=this;
    this.setData({
      //拿到当前索引并动态改变
      currentIndex: e.currentTarget.dataset.idx
    })
  },
  back:function(){
    wx.switchTab({
      url: '/pages/mine/mine'
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },
 //查询用户已开票订单
 queryUserOrderY:function(){
  //获取phone
  const phone = wx.getStorageSync('phone');
  //获取token
  const token = wx.getStorageSync('token');
  wx.request({
    url: 'http://192.168.2.18:9999/productOrder/appUserTipReady?phone='+phone,
    header:{token},
    method:"GET",
    success:(res)=>{
      let resdata = res.data;
      if (resdata.code===200) {
        this.setData({
          dealedList:resdata.data
        })
      }
      console.log(res.data);
    }
  })
},
 //查询用户未开票订单
  queryUserOrderN:function(){
    //获取phone
    const phone = wx.getStorageSync('phone');
    //获取token
    const token = wx.getStorageSync('token');
    wx.request({
      url: 'http://192.168.2.18:9999/productOrder/appUserQueryOrder?phone='+phone,
      header:{token},
      method:"GET",
      success:(res)=>{
        let resdata = res.data;
        if (resdata.code===200) {
          this.setData({
            receivedList:resdata.data
          })
        }
        console.log(res.data);
      }
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    //查询未开票记录订单
    this.queryUserOrderN();
    this.queryUserOrderY();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})